package com.yuju.controller.pub;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yuju.controller.base.BaseController;
import com.yuju.log.model.LogBizRecordModel;
import com.yuju.order.model.ReserveModel;
import com.yuju.order.model.RoomOrderModel;
import com.yuju.rpt.service.IExportForOrderService;
import com.yuju.util.Format;
import com.yuju.util.Utils;

/**
 * 导入导出
 * @author sunyl
 * @update 2016-07-09
 *
 */
@Controller
@RequestMapping("expo")
public class ExportForOrderController extends BaseController {
	@Autowired
	private IExportForOrderService exportForOrderService;
	
	/**
	 * 
	 * 功能：新导出(下载)预离报表Excel<br/>
	 *
	 * @author sunyl
	 * @Date 2018年1月24日 下午3:33:35 <br/>
	 * @return byte数组
	 */
	@RequestMapping(value = "/expCheckoutExcel")
	public void exportCheckoutExcel(RoomOrderModel model) throws Exception {
		HttpServletResponse response = getResponse();
		String excelFileName = "";
		InputStream excelStream = null;
		if (Utils.isNull(model.getBusinessTime())) {
			model.setBusinessTime(new Date());
		}
		byte[] bytes = exportForOrderService.expCheckoutRpt(model);
		ByteArrayInputStream is = new ByteArrayInputStream(bytes);
		excelStream = is; // 文件流
		excelFileName = "预离报表.xlsx"; // 设置下载的文件名

		response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ new String((excelFileName).getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(excelStream);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (final IOException e) {
            throw e;
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
	}
	
	/**
	 * 
	 * 功能：新导出(下载)预抵报表Excel<br/>
	 *
	 * @author sunyl
	 * @Date 2018年1月24日 下午3:33:35 <br/>
	 * @return byte数组
	 */
	@RequestMapping(value = "/expCheckinExcel")
	public void exportCheckinExcel(ReserveModel model) throws Exception {
		HttpServletResponse response = getResponse();
		String excelFileName = "";
		InputStream excelStream = null;
		if (Utils.isNull(model.getBusinessTime())) {
			model.setBusinessTime(new Date());
		}
		
		byte[] bytes = exportForOrderService.expCheckinRpt(model);
		ByteArrayInputStream is = new ByteArrayInputStream(bytes);
		excelStream = is; // 文件流
		excelFileName = "预抵报表.xlsx"; // 设置下载的文件名

		response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ new String((excelFileName).getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(excelStream);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (final IOException e) {
            throw e;
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
	}
	/**
	 * 
	 * 功能：导出(下载)异动报表Excel<br/>
	 *
	 * @author sunyl
	 * @Date 2018年1月24日 下午3:33:35 <br/>
	 * @return byte数组
	 */
	@RequestMapping(value = "/expOrderChangeExcel")
	public void expOrderChangeExcel(LogBizRecordModel model) throws Exception {
		HttpServletResponse response = getResponse();
		String excelFileName = "";
		InputStream excelStream = null;
		byte[] bytes = exportForOrderService.expOrderChangeExcel(model);
		ByteArrayInputStream is = new ByteArrayInputStream(bytes);
		excelStream = is; // 文件流
		excelFileName = "异动报表.xlsx"; // 设置下载的文件名
		
		response.reset();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename="+ new String((excelFileName).getBytes(), "iso-8859-1"));
		ServletOutputStream out = response.getOutputStream();
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(excelStream);
			bos = new BufferedOutputStream(out);
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (final IOException e) {
			throw e;
		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
	}
}
